from stack import Stack


def matches(open, close):
    opens = '([{'
    closes = ')]}'

    # 根据对应的位置判断左右符号是否匹配
    return opens.index(open) == closes.index(close)


def par_checker(symbol_string):
    s = Stack()
    status = True

    index = 0
    while index < len(symbol_string) and status:
        symbol = symbol_string[index]
        if symbol in '([{':
            s.push(symbol)
        else:
            if s.is_empty():
                status = False
            else:
                # 如果不是空值则将栈顶pop出来验证左右符号是否匹配，注意pop出来的是左符号，symbol是右符号
                top = s.pop()
                if not matches(top, symbol):
                    status = False

        index += 1

    if s.is_empty() and status:
        return True
    else:
        return False


if __name__ == "__main__":
    s = '(){}[]'
    print(par_checker(s))